<?php
namespace app\mobile\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Login extends Controller
{
    public function index()
    {
        // 注册页面
        return view('mobile@Login/index');
    }

    /**
     * 登录验证
     * @param Request $request
     * @return string
     * @throws \think\exception\DbException
     */
    public function dologin(Request $request)
    {
        $res = $request->post();
        if ($res['email']) {
        if(strpos($res['email'],'@') !==false){
            $data['email'] = $res['email'];
            $data['password'] = md5($res['password']);
            $list = db('h_user')
                    ->where($data)
                    ->where('status', '1')
                    ->find();
            if($list){
                Session::set('id', $list['id']);
                Session::set('name', $list['name']);
                Session::set('email', $list['email']);
                Session::set('phone', $list['phone']);

                // return $this->redirect('mobile/Index/index');
                return $list;
            }
        }
        }
        return false;
    }

    /**
     * @return string
     */
    public function logOut()
    {
        $res = Session::get();

        session_unset($res);
        return $this->redirect('mobile/Index/index');
//        return view('index@Index/index', [
//            'title'=>'主页'
//        ]);
    }
    public function sms()
    {
        // 注册页面
        return view('mobile@Login/sms');
    }

    public function smslogin(Request $request)
    {
        $res = $request->post();
        // dump(Session::get('verify'));
        // dump($res);
        if (session::get('verify')) {
        
        if ($res['code'] == session::get('verify')) {
            $list = Db::name('h_user')
                ->where('phone',$res['phone'])
                ->where('status','1')
                ->find();
                // dump($list);die;
            if ($list) {
                Session::set('id', $list['id']);
                Session::set('name', $list['name']);
                Session::set('password', md5($list['password']));
                return $list;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }else{
        return false;
    }
    }



}